<?php

namespace App\Observers;

use App\Jobs\OrderExpire;
use App\Jobs\RefundBalance;
use App\Models\LogBalance;
use App\Models\Message;
use App\Models\Order;
use Carbon\Carbon;

class OrderObserver
{
    /**
     * 监听创建前事件.
     *
     * @param Order $model
     * @return void
     */
    public function creating(Order $model)
    {

    }

    /**
     * 监听创建事件.
     *
     * @param Order $model
     * @return void
     */
    public function created(Order $model)
    {
        Message::create([
            'user_id' => $model->user_id,
            'title' => trans('api.order_message'),
            'content' => trans('api.create_order_message', ['minutes' => Order::CLOSE_SECONDS / 60]),
            'type' => Message::ORDER,
        ]);
    }

    public function updating(Order $model)
    {
        if ($model->isDirty('status') && in_array($model->status, Order::$CLOSED_STATUS_MAP)) {
            $model->closed = 1;
        }
    }

    /**
     * 监听更新后事件.
     *
     * @param Order $model
     * @return void
     */
    public function updated(Order $model)
    {
        //订单支付
        if ($model->isDirty('paid')) {
            //记录
            $log['user_id'] = $model->user_id;
            $log['user_balance'] = $model->user->wallet->balance;
            $log['action'] = LogBalance::CONSUME;
            $log['amount'] = $model->total_amount;

            LogBalance::create($log);
        }

        if ($model->paid_at) {
            //支付后 推入订单确认过期队列
            if ($model->isDirty('status') && $model->status == Order::STATUS_PAID && $model->refund_status == Order::REFUND_STATUS_PENDING) {
                $seconds = strtotime($model->schedule->begin_time) + Order::EXPIRE_SECONDS - time();#

                $job = (new OrderExpire($model))->delay(Carbon::now()->addSeconds($seconds));#超过订单预定服务时间15分鐘自动退款

                dispatch($job);

                Message::create([
                    'user_id' => $model->user_id,
                    'title' => trans('api.order_message'),
                    'content' => trans('api.create_order_message'),
                    'type' => Message::ORDER,
                ]);
            }

            //用户申请退款 -- 正常退款流程
            if ($model->isDirty('refund_status') && $model->refund_status == Order::REFUND_STATUS_APPLIED) {

                $job = (new RefundBalance($model))->delay(Carbon::now()->addSeconds(Order::REFUND_SECONDS));#24小时候自动退款

                dispatch($job);

                Message::create([
                    'user_id' => $model->user_id,
                    'title' => trans('api.order_message'),
                    'content' => trans('api.refund_applying'),
                    'type' => Message::ORDER,
                ]);
            }

            //订单完成
            if ($model->isDirty('status') && $model->status == Order::STATUS_FINISH && $model->refund_status == Order::REFUND_STATUS_PENDING) {
                $model->goods->sell_amount += 1;

                $model->goods->save();
            }

            //订单退款成功
            if ($model->isDirty('refund_at')) {
                if ($model->goods->sell_amount > 0) {
                    $model->goods->sell_amount -= 1;
                    $model->goods->save();
                }

                //记录
                $log['user_id'] = $model->user_id;
                $log['user_balance'] = $model->user->wallet->balance;
                $log['action'] = LogBalance::REFUND;
                $log['amount'] = $model->total_amount;

                LogBalance::create($log);

                Message::create([
                    'user_id' => $model->user_id,
                    'title' => trans('api.order_message'),
                    'content' => trans('api.refund_message'),
                    'type' => Message::ORDER,
                ]);
            }
        }
    }

    /**
     * 监听删除事件.
     *
     * @param Order $model
     * @return void
     */
    public function deleting(Order $model)
    {

    }

    /**
     * 监听删除事件.
     *
     * @param Order $model
     * @return void
     */
    public function deleted(Order $model)
    {

    }

}
